package com.simon.app.october.modules.job.service.impl;

import com.simon.app.october.common.utils.Constant;
import com.simon.app.october.common.utils.PageUtil;
import com.simon.app.october.common.utils.QueryPage;
import com.simon.app.october.modules.job.dao.ScheduleJobDao;
import com.simon.app.october.modules.job.entity.ScheduleJobEntity;
import com.simon.app.october.modules.job.service.ScheduleService;
import com.simon.app.october.modules.job.utils.ScheduleUtil;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleJobDao scheduleJobDao;

    @Autowired
    private Scheduler      scheduler;

    @Override
    public ScheduleJobEntity findById(Long jobId) {
        return scheduleJobDao.findById(jobId);
    }

    @Override
    public PageUtil<ScheduleJobEntity> getPageByParams(QueryPage params) {
        int total = scheduleJobDao.getTotalByParams(params);
        int limit = params.getLimit();
        int pageNum = (int) Math.ceil(total/(double)limit);

        PageUtil<ScheduleJobEntity> page = new PageUtil<>();
        page.setTotalCount(total);
        page.setCurrPage(params.getPage());
        page.setPageSize(limit);
        page.setList(scheduleJobDao.getPageByParams(params));
        page.setTotalPage(pageNum);

        return page;
    }

    @Override
    @Transactional
    public void save(ScheduleJobEntity entity) {
        entity.setCreateTime(new Date());
        entity.setStatus(Constant.ScheduleStatus.PAUSE.getValue());
        scheduleJobDao.insert(entity);

        // 创建job
        ScheduleUtil.createScheduleJob(scheduler, entity);
    }

    @Override
    @Transactional
    public void update(ScheduleJobEntity entity) {
        ScheduleUtil.update(scheduler, entity);
        scheduleJobDao.update(entity);
    }

    @Override
    @Transactional
    public void delete(Long[] jobIds) {
        for (long jobId : jobIds) {
            ScheduleUtil.delete(scheduler, jobId);
        }
        scheduleJobDao.delete(Arrays.asList(jobIds));
    }

    /**
     * 立即执行
     * */
    @Override
    public void run(Long[] jobIds) {
        for (long jobId : jobIds) {
            ScheduleUtil.run(scheduler, findById(jobId));
        }
    }

    @Override
    @Transactional
    public void pause(Long[] jobIds) {

        for (long jobId:jobIds) {
            ScheduleUtil.pauseJob(scheduler, jobId);
        }
        // 改变状态
        alterStatus(jobIds, Constant.ScheduleStatus.PAUSE.getValue());
    }

    @Override
    @Transactional
    public void resume(Long[] jobIds) {
        for (long jobId:jobIds) {
            ScheduleUtil.resume(scheduler, jobId);
        }
        // 改变状态
        alterStatus(jobIds, Constant.ScheduleStatus.NORMAL.getValue());
    }

    @Override
    public void alterStatus(Long[] jobIds, int status) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", Arrays.asList(jobIds));
        map.put("status", status);
        scheduleJobDao.alterStatus(map);
    }
}
